local GuildWarLeftPanel = app.mvc.createPanelClass(...)

--data =
function GuildWarLeftPanel.createWithData(data, node)
    local sprite = GuildWarLeftPanel.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end

function GuildWarLeftPanel.create(node)
    return GuildWarLeftPanel.createWithData(nil, node)
end

function GuildWarLeftPanel:init(data)
    self.super.init(self, data)
    self:registerViewEvent({
        {name = ViewEvent.CSGuildWar_SeasonRank, callback = handler(self, self._onSeasonRank)},
    })

    self._ui["Panel/btn_rank"]:addClickEventListener(function()
        UIHandler:handleMessage({id = Constant.FUNC_TYPE_SUPER_RANK,mode = "open", menuType = Enums.SuperRankMenu.CSGuildWar})
    end)
    self._season = -1

    self._ui["Panel/Panel_first"]:setVisible(false)
    self._ui["Panel/btn_rank"]:setVisible(false)
    self._ui["Panel/bg2"]:setVisible(false)
    self._ui["Panel/bg1"]:setVisible(false)
end



function GuildWarLeftPanel:_onSeasonRank(eventName, datas, seasonNow)
    if datas then
        self._champion = datas.guilds[1]
    end
    self._season = seasonNow
    self:refresh()
end

function GuildWarLeftPanel:refresh()
    self._ui["Panel/img_next"]:setVisible(self._season == 0)

    if self._season >=1 then
        local title = string.format(L("CSGuildWarSeason"), self._season)
        self._ui.tlt:setString(title)
        local tS = app:getServerTimeByHMSW(0,0,0,1, false)
        local tS2 = tS + 7 * 3600 * 24 -1
        local t = os.date("*t", tS)
        local t2 = os.date("*t", tS2)
        local dStr = string.format(L("%d.%02d~%d.%02d"), t.month, t.day, t2.month, t2.day)
        self._ui.text_mode:setString(dStr)
    else
        self._ui.tlt:setString("")
        self._ui.text_mode:setString("")
    end

    if self._champion then
        self._ui["Panel/Panel_first"]:setVisible(true)
        self._ui["Panel/btn_rank"]:setVisible(true)
        self._ui["Panel/bg2"]:setVisible(true)
        self._ui["Panel/bg1"]:setVisible(true)
        local leader = self._champion:getLeader()
        self._ui.server:setString(self._champion:getServerName().. " " ..self._champion:getGuildName())
        if leader then
            -- self._ui["Panel/Panel_first/tlt"]:setVisible(true)
            local AvatarFactory = app:getClass("AvatarFactory")
            -- 加载完之后直接创建角色
            local function onCreateCharacter()
                if tolua.isnull(self) then return end
                if not self._champion then
                    return
                end
                self._ui.Node_1:removeAllChildren()
    
                local leader2 = self._champion:getLeader()
                if leader2 then
                    local character = AvatarFactory.createHeroIgnoreMount(leader2)
                    character:playAni({name = "mapidle"})
                    -- character:setScale(0.8)
                    self._ui.Node_1:removeAllChildren()
                    self._ui.Node_1:addChild(character)
                    character:setTag(100)
                end
            end
    
            AvatarFactory.preloadHero(leader, function()
                onCreateCharacter(leader)
            end)
            self._ui["Panel/Panel_first/name"]:setString(leader.name or "")
        else
            self._ui["Panel/Panel_first/name"]:setString("")
        end
    else
        
        self._ui["Panel/Panel_first"]:setVisible(false)
        self._ui["Panel/btn_rank"]:setVisible(false)
        self._ui["Panel/bg2"]:setVisible(false)
        self._ui["Panel/bg1"]:setVisible(false)
    end
end

return GuildWarLeftPanel